package com.incar.chuxing.base;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.incar.chuxing.BuildConfig;
import com.incar.chuxing.R;
import com.incar.chuxing.activity.DeliverCarActivity;
import com.incar.chuxing.activity.DeliverCarMapActivity;
import com.incar.chuxing.activity.MainActivity;
import com.incar.chuxing.activity.PhotoViewActivity;
import com.incar.chuxing.activity.RecogCarPlateActivity;
import com.incar.chuxing.activity.TakePictureActivity;
import com.incar.chuxing.activity.certification.CameraActivity;
import com.incar.chuxing.activity.driving.CarPositionActivity;
import com.incar.chuxing.activity.driving.DrivingActivity;
import com.incar.chuxing.activity.driving.ReturnCarCameraActivity;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.Dialogs.LoadingDialog;
import com.incar.chuxing.utils.SPUtils;
import com.incar.chuxing.utils.StatusBarUtil;
import com.incar.chuxing.utils.ToastUtils;
import com.kingja.loadsir.core.LoadService;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;


public abstract class BaseActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;
    public LoadService loadService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initCustomMapView();
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        AppManager.getAppManager().addActivity(this);
        init();
    }

    public abstract int getLayoutId();


    public void initCustomMapView() {
    }


    private final void init() {
//设置状态栏颜色 或者字体黑白
        if (this instanceof DrivingActivity || this instanceof CarPositionActivity || this instanceof TakePictureActivity || this instanceof DeliverCarMapActivity) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if (StatusBarUtil.MIUISetStatusBarLightMode(getWindow(), true)) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                        getWindow().setStatusBarColor(Color.TRANSPARENT);
                    } else {
                        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    }
                } else if (StatusBarUtil.FlymeSetStatusBarLightMode(getWindow(), true)) {
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//6.0
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    if (!BuildConfig.DEBUG) {
                        getWindow().setStatusBarColor(Color.TRANSPARENT);
                    } else {
                        switch (SPUtils.getInstance(this).getIntValue("$s_urlMode")) {
                            case 1://dev 黄色
                                getWindow().setStatusBarColor(getResources().getColor(R.color.yellow_light));
                                break;
                            case 2://test 绿色
                                getWindow().setStatusBarColor(getResources().getColor(R.color.green_light));
                                break;
                            case 3://prod 透明
                                getWindow().setStatusBarColor(Color.TRANSPARENT);
                                break;
                            default://默认test
                                getWindow().setStatusBarColor(getResources().getColor(R.color.green_light));
                                break;
                        }
                    }
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.0
                    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    if (!BuildConfig.DEBUG) {
                        getWindow().setStatusBarColor(Color.TRANSPARENT);
                    } else {
                        switch (SPUtils.getInstance(this).getIntValue("$s_urlMode")) {
                            case 1://dev
                                getWindow().setStatusBarColor(getResources().getColor(R.color.yellow_light));
                                break;
                            case 2://test
                                getWindow().setStatusBarColor(getResources().getColor(R.color.green_light));
                                break;
                            case 3://prod
                                getWindow().setStatusBarColor(Color.TRANSPARENT);
                                break;
                            default://默认test
                                getWindow().setStatusBarColor(getResources().getColor(R.color.green_light));
                                break;
                        }
                    }
                }
            }

        } else if (this instanceof CameraActivity || this instanceof RecogCarPlateActivity || this instanceof ReturnCarCameraActivity || this instanceof PhotoViewActivity) {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.black_pure), 0, false);
        } else {
            if (!BuildConfig.DEBUG) {
                StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0, true);
            } else {
                switch (SPUtils.getInstance(this).getIntValue("$s_urlMode")) {
                    case 1://dev
                        StatusBarUtil.setColor(this, getResources().getColor(R.color.yellow), 0, true);
                        break;
                    case 2://test
                        StatusBarUtil.setColor(this, getResources().getColor(R.color.green), 0, true);
                        break;
                    case 3://prod
                        StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0, true);
                        break;
                    default://默认test
                        StatusBarUtil.setColor(this, getResources().getColor(R.color.green), 0, true);
                        break;
                }
            }
        }
        loadingDialog = new LoadingDialog(this, true);
        initData();
        initView();
    }

    public abstract void initView();

    public void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }


    @Override
    protected void onDestroy() {
        ToastUtils.reset();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }


    private ProgressDialog progressDialog;

    protected void showBaseProgressDialog(String message) {
        if ((!isFinishing()) && (this.progressDialog == null)) {
            this.progressDialog = new ProgressDialog(this);
        }
        this.progressDialog.setMessage(message);
        this.progressDialog.setCanceledOnTouchOutside(false);
        this.progressDialog.show();
    }

    protected void closeBaseProgressDialog() {
        if (this.progressDialog != null)
            this.progressDialog.dismiss();
    }


    public void showLoadingDialog(String message) {
        if (loadingDialog != null && !loadingDialog.isShowing()) {
            loadingDialog.setTitle(message);
            loadingDialog.show();
        }
    }

    public void showLoadingDialog() {
        if (loadingDialog != null && !loadingDialog.isShowing()) {
            loadingDialog.setTitle(null);
            loadingDialog.show();
        }
    }

    public void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity();
    }
}
